﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Threading;
using LineOfBusiness.Controls.Base;
using LineOfBusiness.Poco;
using LineOfBusiness.DataAccess;

namespace LinOfBusiness.AD.Controllers
{
    public class SetupSequencesController : LOBController
    {
        public SetupSequencesController()
        {
            this.GridViewName = "grvtblSetupSequences";
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            ViewBag.Edit = 0;
            AD_tblSetupSequences SetupSequences = new AD_tblSetupSequences();
            return View("Input", SetupSequences);
        }

        public ActionResult View(string SequenceID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                AD_tblSetupSequences SetupSequences = dal.Repository.GetQuery<AD_tblSetupSequences>().Where(o => o.SequenceID == SequenceID).FirstOrDefault();
                ViewBag.Edit = -1;
                return View("Input", SetupSequences);
            }
        }

        public ActionResult Edit(string SequenceID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                AD_tblSetupSequences SetupSequences = dal.Repository.GetQuery<AD_tblSetupSequences>().Where(o => o.SequenceID == SequenceID).FirstOrDefault();
                if (this.Request.QueryString.AllKeys.Contains("View"))
                    ViewBag.Edit = -1;
                else
                    ViewBag.Edit = 1;

                if (this.ViewData.ContainsKey("copy"))
                    SetupSequences.LastNumber = 0;

                return View("Input", SetupSequences);
            }

        }

        [HttpPost]
        public ActionResult Delete(AD_tblSetupSequences SetupSequences)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    dal.Repository.Delete(SetupSequences);
                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

        [HttpPost]
        public ActionResult Save(AD_tblSetupSequences SetupSequences)
        {
            string method = "Edit";
            string KeyOld = "";

            if (ViewData["Edit"].Equals("0"))
            {
                method = "Add";
            }
            else
            {
                KeyOld = ViewData["ID"] as string;
            }

            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    if (method == "Add")
                    {
                        dal.Repository.Add(SetupSequences);
                    }
                    else
                    {
                        if (KeyOld == SetupSequences.SequenceID)
                        {
                            dal.Repository.Update(SetupSequences);
                        }
                        else
                        {
                            dal.Repository.Delete<AD_tblSetupSequences>(o => o.SequenceID == KeyOld);
                            dal.Repository.Add(SetupSequences);
                        }
                    }

                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

    }
}
